﻿using System;

using OctoOS.UI;
using OctoOS;

public partial class __allArticles : OOSPage
{
	class PathInfo
	{
		[PathInfoMember(0, true)]
		internal int Page = 0;

		void PostInit()
		{
			if(Page > 0)
				Page--;
		}
	}

	PathInfo _pathInfo;
	int _itemsOnPage = 20;

	protected void Page_Init(object sender, EventArgs e)
	{
		_pathInfo = EnablePathInfo<PathInfo>();
	}

    protected void Page_Load(object sender, EventArgs e)
    {
		PageTitle = Resources.Main.allArticles_Title;
		AddBreadcrumb(string.Empty, "/");

		int offset = _itemsOnPage * _pathInfo.Page;
		int totalCount;
		var previews = ApiService.GetMainPreviews(OOSRequest.Current,
			offset, _itemsOnPage, out totalCount);

		allPager.FirstPageUrl = Urls.GetAllArticles();
		allPager.PageUrlPattern = Urls.GetAllArticles() + "/{0}";
		allPager.PageCount = NumericPager.CalculatePageCount(totalCount, _itemsOnPage);
		allPager.SelectedIndex = _pathInfo.Page;

		if(previews.Length == 0)
		{
			empty.Visible = true;
		}
		else
		{
			var categories = AppUtil.GetCategories(OOSRequest.Current);

			foreach(var preview in previews)
			{
				itemsList.Items.AddItem(
					new MainArticleUIPreview(preview, categories));
			}
		}
    }
}